#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json

from kafka import KafkaConsumer
from configparser import ConfigParser

from common import hlog, mq_config_abs_path, common_abs_path
from data_handle.lagou_detail import LagouDetailClean
from data_handle.zhipin_detail import ZhipinDetailClean


def main():
    hlog.enter_func("position_detail_html_consumer.main()")

    config = ConfigParser()
    config.read(mq_config_abs_path.absolute(), encoding='UTF-8')
    host = config['mq_position_detail_html']["host"]
    group = config['mq_position_detail_html']["group"]
    topic = config['mq_position_detail_html']["topic"]

    platform_config = ConfigParser()
    platform_config.read(common_abs_path, encoding='UTF-8')

    while True:
        if sppm.signal_monitor():
            break
        consumer = KafkaConsumer(topic,
                                 group_id=group,
                                 bootstrap_servers=[host])
        for message in consumer:

            sppm.working_lock(True)

            json_string = message.value.decode("utf-8")

            try:
                load_dict = json.loads(json_string)

                if load_dict["platform"] == platform_config["platform"]["LaGou"]:
                    result = LagouDetailClean(load_dict)
                    result.data_clean()
                elif load_dict["platform"] == platform_config["platform"]["ZhiPin"]:
                    result = ZhipinDetailClean(load_dict)
                    result.data_clean()
            except:
                pass

            sppm.working_lock(False)


if __name__ == "__main__":
    import sppm

    sppm.sppm_start(main)
